package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.constants.UserConstants;
import com.heima.model.article.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.utils.common.Desensitization;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2024-08-21
 */
@Service
@RequiredArgsConstructor
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    private final ApUserRealnameMapper apUserRealnameMapper;

    /**
     * 用户审核分页列表
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult auth(AuthDto authDto) {

        //设置分页
        Page page = new Page(authDto.getPage(), authDto.getSize());
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(authDto.getStatus()!=null,ApUserRealname::getStatus, authDto.getStatus());
        page(page, queryWrapper);

        List<ApUserRealname> records = page.getRecords();
        for (ApUserRealname record : records) {

            String idno = record.getIdno();
            String id = Desensitization.idCardDesensitization(idno);
            record.setIdno(id);
        }


        //构造返回对象
        ResponseResult result = new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) page.getTotal());
        result.setData(records);
        return result;
    }


    /**
     * 用户审核失败
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto authDto) {
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(authDto.getId());
        apUserRealname.setStatus(UserConstants.AUTH_FAIL);
        apUserRealname.setReason(authDto.getMsg());
        apUserRealname.setUpdatedTime(LocalDateTime.now());
        apUserRealnameMapper.updateById(apUserRealname);
        return ResponseResult.okResult(apUserRealname);
    }


    /**
     * 用户审核成功
     * @param authDto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult authPass(AuthDto authDto) {
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(authDto.getId());
        apUserRealname.setStatus(UserConstants.AUTH_PASS);
        apUserRealname.setUpdatedTime(LocalDateTime.now());

        //通过id去ap_user表中查询手机号和密码

        //将手机号存入

        apUserRealnameMapper.updateById(apUserRealname);
        return ResponseResult.okResult(apUserRealname);

    }
}
